/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.survey.data;

import com.vng.zing.survey.thrift.model.Response;
import com.vng.zing.survey.thrift.model.ResponseKey;
import com.vng.zing.survey.thrift.model.Error;
import java.io.IOException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import kyotocabinet.DB;
import org.apache.log4j.Logger;

/**
 *
 * @author kienpd
 */
public class ResponseData extends Base {

    private static final Logger logger_ = Logger.getLogger(ResponseData.class);
    private static final Lock createLock_ = new ReentrantLock();
    private static ResponseData _instance = null;

    private final DB db;

    public ResponseData() {
        db = new DB();
        if (!db.open("database/response.kch"+cofigdata  , DB.OWRITER | DB.OCREATE|DB.OAUTOTRAN)) {
            System.err.println("open error: " + db.error());
        }

    }

    public static ResponseData getInstance() {
        if (_instance == null) {
            createLock_.lock();
            try {
                if (_instance == null) {
                    _instance = new ResponseData();
                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instance;

    }

    public Response getResponse(int idSurvey, int idUser){
        Response response = new Response();
        ResponseKey responseKey = new ResponseKey(idSurvey, idUser);

        try {
            byte[] key = serialize(responseKey);
            byte[] value = db.get(key);
            if (value == null) {
                return null;
            }
            response = (Response) deserialize(value);
            return response;
        } catch (IOException ex) {
            logger_.error("ResponseData.getResponse:" + ex.getMessage(), ex);
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ResponseData.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public Error setResponse(int idSurvey, int idUser, Response response) {
        ResponseKey responseKey = new ResponseKey(idSurvey, idUser);
        try {
            byte[] key = serialize(responseKey);
            byte[] value = serialize(response);
            if (!db.set(key, value)) {
                return Error.ERR_SYSTEM;
            }
        } catch (IOException ex) {
            logger_.error("ResponseData.setResponse:" + ex.getMessage(), ex);
            return Error.ERR_SYSTEM;
        }
        return Error.SUCCESS;
    }

    public Error removeResponse(int idSurvey, int idUser) {
        ResponseKey responseKey = new ResponseKey(idSurvey, idUser);
        try {
            byte[] key = serialize(responseKey);

            if (db.seize(key) == null) {
                return Error.ERR_SYSTEM;
            }
            return Error.SUCCESS;
        } catch (IOException ex) {
            logger_.error("ResponseData.removeResponse:" + ex.getMessage(), ex);
        }
        return Error.ERR_SYSTEM;

    }
}
